import java.util.ArrayList;
import java.util.Scanner;

public class BookManagementSystem {
    private static ArrayList<Book> bookList=new ArrayList<>();
    private static Scanner sc=new Scanner(System.in);

    public static void main(String[] args) {
        while (true){
            showMenu();
            int choice=sc.nextInt();
            sc.nextLine();
            switch(choice){
                case 1 -> addBook();
                case 2 -> showAllBook();
                case 3 -> searchBook();
                case 4 -> deleteBook();
                case 5 -> {
                    System.out.println("谢谢使用,再见");
                    return;
                }
                default -> System.out.println("输入错误,请重新输入");
            }
        }
    }
    private static void showMenu(){
        System.out.println("========图书管理系统========");
        System.out.println("1.添加图书");
        System.out.println("2.查看所有图书");
        System.out.println("3.查询图书");
        System.out.println("4.删除图书");
        System.out.println("5.退出系统");
        System.out.println("6.请选择操作");
    }
    private  static void addBook(){
        System.out.println("请输入书名");
        String title=sc.nextLine();
        System.out.println("请输入作者");
        String author=sc.nextLine();
        System.out.println("请输入价格");
        double price=sc.nextDouble();
        bookList.add(new Book(title,author,price));
        System.out.println("添加成功");
    }
    private static void showAllBook(){
        if(bookList.isEmpty()){
            System.out.println("当前图书列表为空");
            return;
        }
        System.out.println("=======所有图书=======");
        for(Book book:bookList){
            System.out.println(book);
        }
    }
    private static void searchBook(){
        System.out.println("请输入要查询的书名");
        String title=sc.nextLine();
        boolean found=false;
        for(Book book:bookList){
            if(book.getTitle().equals(title)){
                System.out.println("查询到的图书信息为:");
                System.out.println(book);
                found=true;
                break;
            }
        }
        if(found==false){
            System.out.println("未查询到该图书");
        }
    }
    private static void deleteBook(){
        System.out.println("请输入要删除的书名");
        String title=sc.nextLine();
        boolean found=false;
        for(int i=0;i<bookList.size();i++){
            if(bookList.get(i).getTitle().equals(title)){
                bookList.remove(i);
                System.out.println("删除成功");
                found=true;
                break;
            }
        }
        if(found==false){
            System.out.println("未查询到该图书");
        }
    }



}